/////////////////////////////////////////////
/// Implementation of the simple template "Tell me * of *"
/////////////////////////////////////////////
sparqlresult=""

// fetching the log
log = context.getLog()

// where can we find infos about the subject
documentQuery = "SELECT DISTINCT ?document "+
	FromPart+
	"WHERE{?s ?p1 ?subjectpattern. ?s <http://www.w3.org/2000/01/rdf-schema#seeAlso> ?document."+
	"FILTER regex(?subjectpattern, '.*"+context.getWildcard(2)+".*', 'i').}"
log.info(documentQuery)
//context.sendMessageToUser(documentQuery)

documents = context.queryForTable(documentQuery,null,null)
FromNamedPart = ""
documents.each(){
	res = it
	log.info(res.getResultCount())
	// res.getResultRows returns a List of Maps. Each map represents a row, with the headline as key.
	res.getResultRows().each(){ 
		//log.info("Hohoho: ${it["ypsilon"]} is a ${it["x"]}")
		FromNamedPart += "FROM <${it["document"]}> \n"
		log.info(it["document"])
	}
}

valueQuery = "SELECT DISTINCT ?value "+
	FromPart+
	FromNamedPart+
	"WHERE {?s ?p1 ?subjectpattern. ?s ?p2 ?value. ?p2 ?property ?propertypattern. FILTER regex(?subjectpattern, '.*"+wildcard2+".*', 'i'). FILTER regex(?propertypattern, '.*"+wildcard1+".*', 'i')}"
log.info(valueQuery)
//context.sendMessageToUser(valueQuery)

counter = 0;
values = context.queryForTable(valueQuery,null,null)
values.each(){
	res = it
	counter = counter + res.getResultCount()
	log.info(res.getResultCount())
	res.getResultRows().each(){ 
		log.info(it["value"])
		context.sendMessageToUser(it["value"])
	}
}
if(counter == 0) context.sendMessageToUser("Sorry, I can't answer your question.")

